package org.springframework.prospring.ticket.web.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.prospring.ticket.service.RegionManager;
import org.springframework.prospring.ticket.service.ClubManager;
import org.springframework.prospring.ticket.service.SurfaceTypeManager;
import org.springframework.prospring.ticket.web.views.JsonView;
import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.domain.Club;
import org.springframework.prospring.ticket.domain.SurfaceType;
import org.springframework.prospring.ticket.domain.ui.Response;
import org.springframework.prospring.ticket.domain.ui.ResponseMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Collection;

@Controller
public class ClubDetailController {
    RegionManager regionManager;
    ClubManager clubManager;
    SurfaceTypeManager surfaceTypeManager;

    public RegionManager getRegionManager() {
        System.out.println("===================== ClubDetailController.getRegionManager");
        return regionManager;
    }

    public void setRegionManager(RegionManager regionManager) {
        System.out.println("===================== ClubDetailController.setRegionManager");
        this.regionManager = regionManager;
    }


    public ClubManager getClubManager() {
        System.out.println("===================== ClubDetailController.getClubManager");
        return clubManager;
    }

    public void setClubManager(ClubManager clubManager) {
        System.out.println("===================== ClubDetailController.setClubManager");
        this.clubManager = clubManager;
    }

    public SurfaceTypeManager getSurfaceTypeManager() {
        System.out.println("===================== ClubDetailController.getSurfaceTypeManager");
        return surfaceTypeManager;
    }

    public void setSurfaceTypeManager(SurfaceTypeManager surfaceTypeManager) {
        System.out.println("===================== ClubDetailController.setSurfaceTypeManager");
        this.surfaceTypeManager = surfaceTypeManager;
    }

    @RequestMapping("/clubDetail.dahtml")
    public ModelAndView showClubDetail(HttpServletRequest req, HttpServletResponse res, HttpSession session, Long provinceIdSelect, Long cityIdSelec, Long regionIdSelect, Long clubId, String razonSocial, String mail) {
        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubDetailController.showClubDetail req = " + req+", provinceIdSelect="+provinceIdSelect+", cityIdSelec="+cityIdSelec+",regionIdSelect="+regionIdSelect+", razonSocial="+razonSocial+", mail="+mail);

        //String regionIdString = "CEN-001";
        //Region region = regionManager.findByBusinessPK(regionIdString);
        //Region region = regionManager.findById(regionIdSelect);


        //System.out.println("region = " + region);

        Club club = clubManager.findClub(clubId);

        System.out.println("club = " + club);

        //Response response = new Response(new ResponseMessage(ResponseMessage.SUCCESS_MESSAGE, "home.htm"));
        //response.addData(new Date(System.currentTimeMillis()));
        //mav.addObject(JsonView.JSON_ROOT, response);
        ModelAndView modelAndView = new ModelAndView("/jsp/clubDetail/clubDetail.jsp");
        modelAndView.addObject("message", "este es un mensaje");
        modelAndView.addObject("club", club);

        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubDetailController.showClubDetail  ModelAndView = " + modelAndView);
        return modelAndView;
    }

    @RequestMapping("/findFielsd4update.dahtml")
    public ModelAndView findFielsd4update(HttpServletRequest req, HttpServletResponse res, HttpSession session,  Long clubId) {
        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubDetailController.findFielsd4update req = " + req+", clubId="+clubId);

        //String regionIdString = "CEN-001";
        //Region region = regionManager.findByBusinessPK(regionIdString);
        //Region region = regionManager.findById(regionIdSelect);

        //System.out.println("region = " + region);

        //Club club = clubManager.findClub(clubId);
        //System.out.println("club = " + club);

        Collection fields = clubManager.findFields(clubId);

        System.out.println("fields = " + fields);

        Collection<SurfaceType> surfaceTypes = surfaceTypeManager.findAllSurfaceTypes();
        System.out.println("surfaceTypes = " + surfaceTypes);
        String surfaceTypeData ="{";
        for (SurfaceType surfaceType: surfaceTypes){
            System.out.println("surfaceType = " + surfaceType);
            surfaceTypeData+= "'" +surfaceType.getId()+ "':'" +surfaceType.getName() +"',";
        }
        surfaceTypeData += "}";
        System.out.println("=======surfaceTypeData = " + surfaceTypeData);
        System.out.println("=======surfaceTypeData = " + surfaceTypeData);

        //List fields = (List) club.getFields();
        //System.out.println("fields = " + fields);

        //Response response = new Response(new ResponseMessage(ResponseMessage.SUCCESS_MESSAGE, "home.htm"));
        //response.addData(new Date(System.currentTimeMillis()));
        //mav.addObject(JsonView.JSON_ROOT, response);
        ModelAndView modelAndView = new ModelAndView("/jsp/club/updateClubFields.jsp");
        modelAndView.addObject("message", "este es un mensaje");
        modelAndView.addObject("fields", fields);
        modelAndView.addObject("surfaceTypeData", surfaceTypeData);
        modelAndView.addObject("surfaceTypes", surfaceTypes);

        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubDetailController.findFielsd4update  ModelAndView = " + modelAndView);
        return modelAndView;
    }

}
